<?php
require_once($_SERVER['DOCUMENT_ROOT'] . '/all.conf.php');
require_once(__PHY_LIB.'/inc/page.inc.php');
require_once(__PHY_LIB.'/class/uUser.class.php');

/// 跳回地址 ///
$sBackUrl = get_back_url('/');
$oTpl->assign('sBack', $sBackUrl);

/// 登录时立即跳转 ///
if ('user' == $_The['UserType'] && 0 < $_The['User']['id']) {
	header("Location: {$sBackUrl}"); exit();
}

/// 初始化 ///
$rReq = validate($_REQUEST, 'action:e[none,check,register]');
$sAction = $rReq['action'];
$rMsg = array();

if ('check' == $sAction) {
	do {
		$rP = validate($_GET, 'field:u|value:u');
		if ('acct' == $rP['field']) {
			if (!$rP['value']) { print('邮箱为空'); break; }
			if (!strpos($rP['value'], '@')) { print('邮箱错误'); break; }
		}
		elseif ('pswd' == $rP['field']) {
			if (!$rP['value']) { print('密码为空'); break; }
		}
		elseif ('nick' == $rP['field']) {
			if (!$rP['value']) { print('昵称为空'); break; }
		}
	} while (false);
	exit(); // check状态下，立即输出错误信息
}
elseif ('register' == $sAction) {
	do {
		$rP = validate($_POST, 'acct:u|pswd:u|rpt_pswd:u|nick:u|agree:e[no,yes]');
		$oTpl->assign('rP', $rP);
		if (!$rP['acct']) { $rMsg['acct'] = '邮箱为空'; }
		elseif (!strpos($rP['acct'], '@')) { $rMsg['acct'] = '邮箱格式错误'; }
		if (!$rP['pswd']) { $rMsg['pswd'] = '密码为空'; }
		if (!$rP['rpt_pswd']) { $rMsg['rpt_pswd'] = '重复密码为空'; }
		if ($rP['rpt_pswd'] != $rP['pswd']) { $rMsg['rpt_pswd'] = '重复密码不一致'; }
		if (!$rP['nick']) { $rMsg['nick'] = '昵称为空'; }
		/// if ('yes' != $rP['agree']) { $rMsg['agree'] = '请同意条款'; }
		if (0 < sizeof($rMsg)) { break; }
		$oUser = new uUser();
		$oUser->set('email', $rP['acct']);
		$oUser->set('nick', $rP['nick']);
		$oUser->set('pswd', $oUser->pswdEncode($rP['pswd']));
		$oUser->set('time_added', date('Y-m-d H:i:s'));
		if ($oUser->insert()) {
			$rMsg['action'] = '注册成功';
			uUser::login($rP['acct'], $rP['pswd'], $rP['long']);
			header("Location: {$sBackUrl}"); exit();
		}
		else {
			$rMsg['acct'] = '注册失败，邮箱或者昵称已有人使用';
			$rMsg['nick'] = '注册失败，邮箱或者昵称已有人使用';
		}
	} while (false);
}

$oTpl->assign('rMsg', $rMsg);
$oTpl->display(dirname(__FILE__).'/'.basename(__FILE__, '.php').'.tpl');
?>
